/**
 *
 */
Ext.define('vacake.view.ShoppingBalance', {
    extend: 'Ext.Container',
    xtype: 'shoppingBalanceView',
    requires: [
        'Ext.ux.field.DateTimePicker'
    ],
    config: {
        layout: 'vbox',
        items: [
            {
                xtype: 'toolbar',
                docked: 'top',
                title: '结算',
                items: [
                    {  text: '返回', id: 'back_Shopping', ui: 'action-back'}
                ]
            },
            {
                xtype: 'formpanel',
                layout: 'vbox',
                id: 'ordersFrom',
                style:'margin:5px 0px',
                flex: 1,
                items: [
                    {
                        cls:'fenge',
                        style:'margin:10px 0'
                    },
                    {
                        cls:'font2 color2 center',
                        html:'订单详情'
                    },
                    {
                        //xtype: 'fieldset',
                        cls:'shoppingEdit_bg shadow radius font2 color2',
                        layout: 'vbox',
                        id: 'showCartInfo'

                    },
                    {
                        cls:'fenge',
                        style:'margin:10px 0'
                    },
                    {
                        cls:'font2 color2 center',
                        html:'输入订单信息'
                    },
                    {
                        xtype: 'fieldset',
                        cls:'x-container5',
                        layout: 'vbox',
                        defaults: {
                            labelWidth: '66px'
                        },
                        items: [
                            {
                                xtype: 'textfield',
                                name: 'orderer',
                                id: 'orderer',
                                label: '订货人:',
                                autoCapitalize: true
                            },
                            {
                                xtype: 'textfield',
                                name: 'otel',
                                id: 'otel',
                                label: '订货手机:',
                                autoCapitalize: true
                            },
                            {
                                xtype: 'selectfield',
                                name: 'selectConsignee',
                                id: 'selectConsignee',
                                options: [
                                    {text: '请选择收货人', value: ''}
                                ],
                                listeners: {
                                    change: function (select, value) {
                                        if (value != '') {
                                            var usersAddress = Ext.getStore('UsersAddress');
                                            var usersAddressItem = usersAddress.getAt(usersAddress.find('csaid', value));
                                            Ext.getCmp("consignee").setValue(usersAddressItem.get('name'));
                                            Ext.getCmp("ctel").setValue(usersAddressItem.get('mobile'));
                                            Ext.getCmp("takeaddress").setValue(usersAddressItem.get('address'));
                                        }else{
                                            Ext.getCmp("consignee").setValue('');
                                            Ext.getCmp("ctel").setValue('');
                                            Ext.getCmp("takeaddress").setValue('');
                                        }
                                    }
                                }
                            },
                            {
                                xtype: 'textfield',
                                name: 'consignee',
                                id: 'consignee',
                                label: '收货人:',
                                autoCapitalize: true
                            },
                            {
                                xtype: 'textfield',
                                name: 'ctel',
                                id: 'ctel',
                                label: '收货手机:',
                                autoCapitalize: true
                            },
                            {
                                xtype: 'textareafield',
                                name: 'takeaddress',
                                id: 'takeaddress',
                                label: '收货地址:',
                                autoCapitalize: true
                            },
                            {
                                xtype: 'datepickerfield',
                                name: 'takedate',
                                id: 'takedate',
                                label: '收货日期:',
                                dateFormat: 'Y-m-d',
                                value: new Date(),
                                picker: {
                                    yearFrom: (new Date().getFullYear() - 5),
                                    yearTo: (new Date().getFullYear() + 5),
                                    slotOrder: ['year', 'month', 'day'],
                                    doneButton: {text: '完成'},
                                    cancelButton: {text: '取消'}
                                }
                            },
                            {
                                xtype: 'datetimepickerfield',
                                name: 'taketime',
                                id: 'taketime',
                                label: '收货时间:',
                                value: new Date(),
                                dateTimeFormat: 'H:i',
                                picker: {
                                    yearFrom: (new Date().getFullYear() - 5),
                                    minuteInterval: 10,
                                    slotOrder: ['hour', 'minute'],
                                    doneButton: {text: '完成'},
                                    cancelButton: {text: '取消'}
                                }
                            },
                            {
                                xtype: 'textfield',
                                name: 'remark',
                                id: 'remark',
                                label: '餐具配置:',
                                placeHolder: '是否要生日牌,餐盘的数量',
                                autoCapitalize: true
                            },
                            {
                                xtype: 'textareafield',
                                name: 'messages',
                                id: 'messages',
                                label: '我要留言:',
                                autoCapitalize: true
                            },
                            {
                                xtype: 'hiddenfield',
                                name: 'cadid',
                                id: 'hidden_cadid'
                            },
                            {
                                xtype: 'hiddenfield',
                                name: 'cuid',
                                id: 'hidden_cuid'
                            },
                            {
                                xtype: 'hiddenfield',
                                name: 'cartids',
                                id: 'hidden_cartids'
                            },
                            {
                                xtype: 'hiddenfield',
                                name: 'totalmoney',
                                id: 'hidden_totalmoney'
                            }
                        ]
                    } ,
                    {
                        cls:'fenge',
                        style:'margin:10px 0'
                    },
                    {
                        cls:'font2 color2 center',
                        html:'支付方式'
                    },
                    {
                        xtype: 'panel',
                        cls:'x-area1',
                        layout: 'vbox',
                        defaults: {
                            labelWidth: '116px'
                        },
                        items: [
                            { xtype: 'radiofield',
                                name: 'paymenttype',
                                label: '货到付款',
                                checked: true,
                                value: '货到付款'
                            },
                            { xtype: 'radiofield',
                                name: 'paymenttype',
                                label: '在线支付/支付宝',
                                value: '在线支付'
                            }
                        ]
                    } ,
                    {
                        cls:'fenge',
                        style:'margin:10px 0'
                    },
                    {
                        cls:'font2 color2 center',
                        html:'配送方式'
                    },
                    {
                        xtype: 'panel',
                        cls:'x-area1',
                        layout: 'vbox',
                        defaults: {
                            labelWidth: '116px'
                        },
                        items: [
                            { xtype: 'radiofield',
                                name: 'picktype',
                                label: '厂家送货',
                                checked: true,
                                value:'配送'
                            },
                            { xtype: 'radiofield',
                                name: 'picktype',
                                label: '自提',
                                value:'自提'
                            }
                        ]
                    } ,
                    {
                        xtype: 'panel',
                        layout: {type: 'hbox', align: 'middle', pack: 'center'},
                        items: [
                            {
                                xtype: 'button',
                                ui: 'confirm',
                                id: 'indentSubmit',
                                width: '96%',
                                text: '提交订单',
                                style: 'margin:0 2% 20px 2%;height:45px;'
                            }
                        ]
                    }
                ]
            }

        ],
        listeners: {
            painted: function () {
                var shoppingCart = Ext.getStore('Shopping');
                shoppingCart.load({
                    params: {
                        cuid: window.localStorage.getItem('app_Login_id'),
                        cadid: window.localStorage.getItem('app_district_id')
                    },
                    callback: function (data) {
                        var shoppingList = [];
                        var carCountPrice = 0;
                        var carCountNumber = 0;
                        var shoppingIds = [];
                        shoppingCart.each(function (shopping) {
                            carCountPrice = carCountPrice + parseFloat(shopping.get('countprice'));
                            carCountNumber = carCountNumber + parseInt(shopping.get('pnumber'));
                            shoppingIds.push(shopping.get('cscid'));
                            shoppingList.push({
                                xtype: 'panel',
                                layout: 'vbox',
                                items: [
                                    {
                                        xtype: 'panel',
                                        layout: 'hbox',
                                        items: [
                                            {
                                                xtype: 'label',
                                                cls:'font1 color2 height30',
                                                width: '40%',
                                                html: '' + shopping.get('pname')
                                            },
                                            {
                                                xtype: 'label',
                                                cls:'font1 color2 height30 center',
                                                width: '20%',
                                                html: '' + shopping.get('sname')
                                            },
                                            {
                                                xtype: 'label',
                                                cls:'font1 color2 height30 right',
                                                width: '40%',
                                                html: '￥' + shopping.get('price')
                                            }
                                        ]
                                    },
                                    {
                                        xtype: 'panel',
                                        layout: 'hbox',
                                        items: [
                                            {
                                                xtype: 'label',
                                                width: '40%',
                                                cls:'font1 color2 height30',
                                                html: ''
                                            },
                                            {
                                                xtype: 'label',
                                                cls:'font1 color2 height30 center',
                                                width: '20%',
                                                html: '' + shopping.get('pnumber')
                                            },
                                            {
                                                xtype: 'label',
                                                cls:'font2 color5 height30 right',
                                                width: '40%',
                                                html: '￥' + shopping.get('countprice')
                                            }
                                        ]
                                    }
                                ]
                            });
                        });
                        shoppingList.push({
                            xtype: 'panel',
                            layout: 'hbox',
                            items: [
                                {
                                    xtype: 'label',
                                    cls:'font2 color2 height30',
                                    width: '50%',
                                    html: '总数：' + carCountNumber
                                },
                                {
                                    xtype: 'label',
                                    width: '50%',
                                    cls:'font5 color8 height30 right',
                                    html: '￥' + carCountPrice
                                }
                            ]
                        });
                        Ext.getCmp('showCartInfo').setItems(shoppingList);
                        Ext.getCmp('hidden_cartids').setValue(shoppingIds);
                        Ext.getCmp('hidden_totalmoney').setValue(carCountPrice);
                        Ext.getCmp('hidden_cadid').setValue(window.localStorage.getItem('app_district_id'));
                        Ext.getCmp('hidden_cuid').setValue(window.localStorage.getItem('app_Login_id'));
                    }});
                Ext.getCmp('orderer').setValue(window.localStorage.getItem('app_Login_name'));
                Ext.getCmp('otel').setValue(window.localStorage.getItem('app_Login_name'));

                var usersAddress = Ext.getStore('UsersAddress');
                usersAddress.load({params: {cuid: window.localStorage.getItem('app_Login_id'),
                    cadid: window.localStorage.getItem('app_district_id')}, callback: function (data) {
                    var msgArray = [
                        {text: '请选择收货人', value: ''}
                    ];
                    Ext.each(data, function (usersAddress) {
                        msgArray.push({text: usersAddress.get('name') + ',' + usersAddress.get('address'), value: usersAddress.get('csaid')})
                    });
                    Ext.getCmp('selectConsignee').setOptions(msgArray);
                }
                });

            },
            back: function () {
                var backid = Ext.Viewport.getInnerItems().length - 2;
                Ext.Viewport.remove(Ext.Viewport.getActiveItem(), true);
                Ext.Viewport.setActiveItem(backid);
            }
        }

    }

});

